/**
 * 生成 link node
 * @param url url
 * @param text text
 */

// 插入文章的自定义菜单
import { IDomEditor, IButtonMenu, SlateNode, SlateElement, SlateEditor, DomEditor } from '@wangeditor/editor';
import { IExtendConfig } from '../interface';
import { PENCIL_SVG } from '../../icon-svg';
import { LinkElement } from '../../custom-types'

// import { isLinkDisabled } from '../../isDisabledMenusConfig';


function getInsertLinkConfig(editor: IDomEditor) {
  const { EXTEND_CONF } = editor.getConfig();
  const { insertLinkConfig } = EXTEND_CONF as IExtendConfig;
  return insertLinkConfig;
}


class EditLinkMenu implements IButtonMenu {
  tag: string;
  title: string;
  iconSvg: string;
  selectedLinkNode: SlateNode | null = null;

  constructor() {
    this.title = '修改链接';
    this.iconSvg = PENCIL_SVG;
    this.tag = 'button';
    this.selectedLinkNode = null;
  }

  private getSelectedLinkElem(editor: IDomEditor): LinkElement | null {
    const node = DomEditor.getSelectedNodeByType(editor, 'link')
    if (node == null) return null
    return node as LinkElement
  }
  /**
   * 获取 node.url
   * @param editor editor
   */
  getValue(editor: IDomEditor): string | boolean {
    const linkElem = this.getSelectedLinkElem(editor)
    if (linkElem) {
      return linkElem.url || ''
    }
    return ''
  }

  // 菜单是否需要激活（如选中加粗文本，“加粗”菜单会激活），用不到则返回 false
  isActive(editor: IDomEditor): boolean {
    // TS 语法
    return false;
  }

  // 菜单是否需要禁用（如选中 H1 ，“引用”菜单被禁用），用不到则返回 false
  isDisabled(editor: IDomEditor): boolean {
    if (editor.selection == null) return true

    const linkElem = this.getSelectedLinkElem(editor)

    // 未匹配到 link node 则禁用
    if (linkElem == null) return true
    return false
  }

  // 点击菜单时触发的函数
  exec(editor: IDomEditor, value: string | boolean) {
    if (this.isDisabled(editor)) return;
    console.log('点击了自定义菜单-修改链接');
    const nodeEntries = SlateEditor.nodes(editor, {
      match: (node) => {
        if (SlateElement.isElement(node)) {
          return true;
        }
        return false;
      },
      universal: true,
    });
    for (const nodeEntry of nodeEntries) {
      const [node] = nodeEntry;
      this.selectedLinkNode = node;
    }
    // console.log('点击了编辑菜单：', this.selectedLinkNode);
    // console.log('=selectedLinkNode22=', this.selectedLinkNode);
    const target = this.selectedLinkNode?.target;
    console.log('编辑菜单：', target);
    let type = 'customlink-edit';
    switch (target) {
      case '_topic':
        type = 'topic';
        break;
      case '_at':
        type = 'at';
        break;
      default:
        if (target.indexOf('article') > -1) {
          type = 'article-edit';
        }
        break;
    }
    console.log('菜单类型', type);

    const linkText = this.selectedLinkNode?.children[0].text;
    const linkAddress = this.selectedLinkNode?.url;
    const articleId = target.replace(/^article_/, '');
    const linkType = articleId ? 'article-edit' : 'customlink-edit';

    // 这里需要判断是文章链接还是普通链接
    const articleParam = {
      linkText,
      linkAddress,
      articleId,
      type,
      target,
      linkType,
    };
    const { showInsertArticleModal } = getInsertLinkConfig(editor);
    showInsertArticleModal(editor,linkType,articleParam);
    // editor.emit('updateCustomLink', {
    //   type,
    //   params: articleParam,
    // });
  }
}

export default EditLinkMenu;
